//
//  JSSStackToolBarView.swift
//  WeiBoItem
//
//  Created by jss on 16/4/26.
//  Copyright © 2016年 jss. All rights reserved.
//

import UIKit


//定义枚举 以区分不同的Button
enum JSSStackToolBarBtnType: Int {
    
    case Picture = 0,Mention,trend,emoticon,add

}

class JSSStackToolBarView: UIStackView {
    
    
    var isSystemKeyboard:Bool = true {
        
        didSet{
        let btn = self.viewWithTag((JSSStackToolBarBtnType.emoticon).rawValue) as? UIButton
            
            var name = "compose_keyboardbutton_background"
            
            
            if isSystemKeyboard {
                //显示笑脸键盘
//                btn?.setImage(UIImage(named: "compose_emoticonbutton_background"), forState: UIControlState.Normal)
//                btn?.setImage(UIImage(named: "compose_emoticonbutton_background_highlighted"), forState: UIControlState.Highlighted)
                
                 name = "compose_emoticonbutton_background"
            }
//            else {
//            
//                //显示系统键盘
//                btn?.setImage(UIImage(named: "compose_keyboardbutton_background"), forState: UIControlState.Normal)
//                btn?.setImage(UIImage(named: "compose_keyboardbutton_background_highlighted"), forState: UIControlState.Highlighted)
//            
//            }
            btn?.setImage(UIImage(named: name), forState: UIControlState.Normal)
            btn?.setImage(UIImage(named: "\(name)_highlighted"), forState: UIControlState.Highlighted)
      
        }
        
    }
    
    //定义一个闭包用来传 按钮点击的方法
    var didClickBtnClosure:((type:JSSStackToolBarBtnType)->())?

  override init(frame: CGRect) {
        super.init(frame: frame)
    
       setupUI()
    }
  
  required init?(coder aDecoder: NSCoder) {
      fatalError("init(coder:) has not been implemented")
  }
    
    private func  setupUI() {
        
        //添加子控件
        addChildBtn("compose_toolbar_picture",type:.Picture)
        addChildBtn("compose_mentionbutton_background",type:.Mention)
        addChildBtn("compose_trendbutton_background",type:.trend)
        addChildBtn("compose_emoticonbutton_background",type:.emoticon)
        addChildBtn("compose_add_background",type:.add)
        
        //设置stackView的分布样式(相等填充满)
        self.distribution = .FillEqually
        
    }
    
    
    //方法添加Button子控件
    private func addChildBtn(imageName:String,type:JSSStackToolBarBtnType) {
        
        let button = UIButton()
        
        button.tag = type.rawValue
        
        //给Button添加监听事件
        button.addTarget(self, action: "didClickBtn:", forControlEvents: UIControlEvents.TouchUpInside)
        
        //设置不同状态下的图片
        button.setImage(UIImage(named: imageName), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "\(imageName)_highlighted"), forState: UIControlState.Highlighted)
        
        //设置不同状态下的背景图片
        button.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named:"compose_toolbar_background"), forState: UIControlState.Highlighted)
        
        //添加到stackView上
        addArrangedSubview(button)
 
    }
    
    //实现按钮点击方法(因为stackview没有监听按钮点击的能力 所以方法必须在控制器里面执行 这里用闭包传递到控制器里面)
    @objc private func didClickBtn(btn:UIButton){
    
        //闭包回调
        didClickBtnClosure?(type:JSSStackToolBarBtnType(rawValue: btn.tag)!)
 
    }

}
